#include <stdio.h>
#include <string.h>

#include "Log_Client.h"

#include "TCPInterface.h"

bool TriggerFunc(LOG_COMM_STU &commuStu);
int TestTcpAsClient();

int main()
{
    printf("Log Test\n");

    // TestTcpAsClient();

    // return 0;

    // AddLog(ERROR_LEVEL, "Test Log Demo", "This is test by LJH,date = %s, time = %s", "24/50/22", "21:16");

    // std::string strResult;
    // QueryLog(5, "", strResult);
    // // ClearLog();
    // DeleteLog(int nLogCount);

    // printf("AFter QueryLog()=============\n");
    // printf("[strRult] = \n%s\n", strResult.c_str());


    LOG_COMM_STU commuStu;
    while (1)
    {
        TriggerFunc(commuStu);
    }
    

    return 0;


}

// TriggerFunc is used to trigger the operation in test, and the function reture output in parameter "commuStu"
bool TriggerFunc(LOG_COMM_STU &commuStu)
{
    char inputBuf[50];
    printf("=============================================================\n");
    printf("printf input a num (0) add   (1) del    (2)clear    (3) query\n");
    fgets(inputBuf,50,stdin);
    

    LOG_LEVEL nLogLevel = DEBUG_LEVEL;
    std::string strResult;

    int typeNum = atoi(inputBuf);
    switch (typeNum)
    {
    case OPT_ADD_LOG:
        AddLog(nLogLevel, "Test", "This is test by LJH,date = %s, time = %s", "24/50/22", "21:16");
        break;
    case OPT_DEL_LOG:
        DeleteLog(nLogLevel);
        break;
    case OPT_CLEAR_LOG:
        ClearLog();
        break;
    case OPT_QUERY_LOG:
        QueryLog(10, "", strResult);
        printf("[strRult] = \n%s\n", strResult.c_str());
        break;
    default:
        return false;
    }


    return 0;
}

// TestTcpAsClient()
int TestTcpAsClient()
{

    
    struct sockaddr_in ServerInfo;
    memset(&ServerInfo, 0 ,sizeof(ServerInfo));
	
	ServerInfo.sin_family = AF_INET;				//协议
	ServerInfo.sin_port = htons(8888);				//端口号
	ServerInfo.sin_addr.s_addr = inet_addr("127.0.0.1");




    while(1)
    {

        int m_socketFd = SocketInitTCPClient(); // socket

        int ret = connect(m_socketFd, (struct sockaddr *)&ServerInfo, sizeof(ServerInfo));
        if(ret == -1)
        {
            printf("connect error\n");
        }

        char cstrData[128] = "Hello";
        ret = send(m_socketFd, cstrData, sizeof(cstrData), 0);
        if (ret <= 0)
        {
            printf("sendto fail ret = %d\n", ret);
            return false;
        }



        // // 关闭写操作
        // if (shutdown(m_socketFd, SHUT_WR) < 0) 
        // {
        //     perror("shutdown");
        //     close(m_socketFd);
        //     return -1;
        // }

        close(m_socketFd);

        sleep(1);
        printf("7997777777777777\n");
    }



    // ====================================================================
    return 0;
}